package fr.afcepf.ticketing.dao;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import fr.afcepf.ticketing.domain.Address;
import fr.afcepf.ticketing.domain.CreditCard;
import fr.afcepf.ticketing.domain.Customer;
import fr.afcepf.ticketing.domain.Order;
import fr.afcepf.ticketing.domain.OrderLine;
import fr.afcepf.ticketing.service.CartTicket;

@Stateless
@Local
public class OrderDaoJpa extends GenericDaoJpa<Order, Integer> implements
		OrderDao {

	@PersistenceContext
	private EntityManager em;

	@PostConstruct
	public void initEntityManagerOfGenericDaoJpa() {
		super.setEntityManager(em);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<Order> getAllOrders() {
		return em.createQuery("select o from Order as o").getResultList();
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<Order> getOrdersOfClient(Long numCli) {
		List<Order> orders = new ArrayList<Order>();

		Query query = em.createQuery("select o from Order as o"
				+ " where o.customer.id=:numCli");
		query.setParameter("numCli",numCli);;
		orders = query.getResultList();
		return orders;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public Collection<Order> getOrdersOfClient(Long numCli, Date date) {
		List<Order> orders = new ArrayList<Order>();

		Query query = em.createQuery("select o from Order as o"
				+ " where o.customer.id=:numCli and o.date=:date");
		query.setParameter("numCli", numCli);
		query.setParameter("date", date);
		orders = query.getResultList();
		return orders;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<OrderLine> editOrderLineByOrder(Integer num) {
		Collection<OrderLine> orderLines = new ArrayList<OrderLine>();
		Query query = em.createQuery("select ol from OrderLine as ol"
				+ " where ol.order.id=:num");
		query.setParameter("num", num);
		orderLines = query.getResultList();
		return orderLines;
	}

	@Override
	public Order createOrder(Customer c, Address ad, CreditCard cb,
			List<CartTicket> cartTickets) {
		List<OrderLine> orderLines = null;
		orderLines = listOrderLines(cartTickets);
		Order order = new Order();
		order.setDate(new Date());
		order.setAddress(ad);
		order.setCreditCard(cb);
		order.setCustomer(c);
		order.setOrderLines(orderLines);
		for(OrderLine ol : orderLines){
			ol.setOrder(order);
		}
		
		em.persist(order);
		return order;
	}

	@Override
	public List<OrderLine> listOrderLines(List<CartTicket> cartTickets) {
		List<OrderLine> orderLines = new ArrayList<OrderLine>();

		for (CartTicket cartTicket : cartTickets) {
			OrderLine orderLine = new OrderLine();
			orderLine.setQuantity(cartTicket.getQuantity());
			orderLine.setUnitprice(cartTicket.getTicket().getPrice());
			orderLine.setItem(cartTicket.getTicket().getName());
			orderLines.add(orderLine);
		}

		orderLines.size();
		return orderLines;
	}



}
